package com.frogs.ucenter.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.PageList;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.ucenter.component.UserPermissionHelper;
import com.frogs.ucenter.entity.SubFlowInfo;
import com.frogs.ucenter.entity.TotalFlowInfo;
import com.frogs.ucenter.entity.UserBaseAccountInfo;
import com.frogs.ucenter.service.TaskService;

/**
 * 优惠劵
 * @author 
 *
 */
@Controller
public class TaskController extends BaseController3 {

	private static Logger log = LoggerFactory.getLogger(TaskController.class);

	//任务内容（建议用ajax返回）
	private static final String TASK_DETAIL = "/task/detail";
	
	//任务激活（建议用ajax返回）
	private static final String TASK_CLAIME = "/task/claime";
	
	//任务完成（建议用ajax返回）
	private static final String TASK_FINISH = "/task/finish";
	
	//常规-任务
	private static final String SINGLE_TASK_LIST = "/single/task/index";
	private static final String SINGLE_TASK_LIST_VIEW = "flow/single_task_list";
	private static final String SINGLE_TASK_PROGRESS = "/flow/singleTaskProgress";
	
	//主线（成就）-子线-任务
	private static final String FLOW_TASK_LIST = "/flow/sub/task/index";
	private static final String FLOW_TASK_LIST_VIEW = "flow/task_list";
	
	//主线（成就）-子线
	private static final String FLOW_SUB_LIST = "/flow/sub/index";
	private static final String FLOW_SUB_LIST_VIEW = "flow/sub_list";
	
	//主线（成就）
	private static final String FLOW_TOTAL_LIST = "/flow/total/index";
	private static final String FLOW_TOTAL_LIST_VIEW = "flow/total_list";
	private static final String TASK_COMPLETE_RATE = "/flow/task/complete/rate";
	
	//主线任务获取
	private static final String GET_TASK = "/task/get";
	
	//首页
	private static final String HOME = "/flow/index";
	private static final String HOME_VIEW = "flow/index";
	
	@Resource
	private UserPermissionHelper userPermissionHelper;
	
	@Resource
	private TaskService taskService;
	
	//主线默认值
	private static final Integer totalId = 3;
	
	
	
	@RequestMapping(HOME)
    public String home(HttpServletRequest request, ModelMap model) {
		try {
			
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			taskService.startTask(user.getId(), totalId, user.getUserType());
		} catch (Exception e) {
			log.error("func[home] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			
			request.setAttribute("errorMessage", e.getMessage());
			return ERROR_VIEW;
		}
		return HOME_VIEW;
    }
	
	/**
	 * 暂时不用实现
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(FLOW_TOTAL_LIST)
    public String flowTotalList(HttpServletRequest request, ModelMap model) {
    	try {
    		HttpRequestInfo reqInfo = new HttpRequestInfo(request);
    		Integer pageNo = reqInfo.getIntParameter("pageNo", 1);
    		Integer pageSize = reqInfo.getIntParameter("pageSize", 3);
    		
    		
    		UserBaseAccountInfo user = userPermissionHelper.getUser(request);
    		PageList<TotalFlowInfo> list = null;
    		model.addAttribute("list","" );
    		model.addAttribute("pageTurn","" );
		} catch (Exception e) {
			log.error("func[flowTotalList] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			
			request.setAttribute("errorMessage", e.getMessage());
			return ERROR_VIEW;
		}
    	return FLOW_TOTAL_LIST_VIEW;
    }
	
	/**
	 * uid为空，则查sub_flow_info 系统提供的子线
	 * uid不为空，则查sub_flow_complete_info 我已经选的子线
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(FLOW_SUB_LIST)
    public String flowSubList(HttpServletRequest request, ModelMap model) {
		try {
    		HttpRequestInfo reqInfo = new HttpRequestInfo(request);
    		Integer pageNo = reqInfo.getIntParameter("pageNo", 1);
    		Integer pageSize = reqInfo.getIntParameter("pageSize",20);
    		
    		UserBaseAccountInfo user = userPermissionHelper.getUser(request);
    		PageList<Map<String, Object>> list = taskService.getSubFlowList(totalId, user.getId(), 0, pageNo, pageSize);
    		model.addAttribute("list",list );
    		model.addAttribute("pageTurn",list.getPageTurn() );
		} catch (Exception e) {
			log.error("func[flowSubList] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			
			request.setAttribute("errorMessage", e.getMessage());
			return ERROR_VIEW;
		}
    	return FLOW_SUB_LIST_VIEW;
    }
	
	/**
	 * uid为空，则查sub_flow_info 系统提供的任务
	 * uid不为空，则查sub_flow_complete_info 我已经选的任务
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(FLOW_TASK_LIST)
    public String flowTaskList(HttpServletRequest request, ModelMap model) {
		try {
    		HttpRequestInfo reqInfo = new HttpRequestInfo(request);
    		int subId = reqInfo.getIntParameter("subId", 1);
    		Integer pageNo = reqInfo.getIntParameter("pageNo", 1);
    		Integer pageSize = reqInfo.getIntParameter("pageSize",10);
    		UserBaseAccountInfo user = userPermissionHelper.getUser(request);
    		
    		JSONObject jsonObjectResult = taskService.getFlowTaskInfoList(subId, user.getId(), pageNo, pageSize);
    		if (jsonObjectResult != null) {
    			List<Map<String, Object>> list = jsonObjectResult.getJSONObject("response").getObject("list", List.class);
    	    	SubFlowInfo subFlowInfo = jsonObjectResult.getJSONObject("response").getObject("info", SubFlowInfo.class);
    	    	model.addAttribute("subFlowInfo",subFlowInfo );
    	    	model.addAttribute("list",list );
    		}
		} catch (Exception e) {
			log.error("func[flowTaskList] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			
			request.setAttribute("errorMessage", e.getMessage());
			return ERROR_VIEW;
		}
    	return FLOW_TASK_LIST_VIEW;
    }
	
	@RequestMapping(SINGLE_TASK_LIST)
    public String singleTaskList(HttpServletRequest request, ModelMap model) {
		try {
			HttpRequestInfo reqInfo = new HttpRequestInfo(request);
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
    		Integer pageNo = reqInfo.getIntParameter("pageNo", 1);
    		Integer pageSize = reqInfo.getIntParameter("pageSize",10);
    		
    		PageList<Map<String, Object>> list = taskService.getPointTaskInfoList( user.getId(), user.getUserType(),pageNo,pageSize);
    		model.addAttribute("list", list);
    		model.addAttribute("pageTurn", list.getPageTurn());
		} catch (Exception e) {
			log.error("func[singleTaskList] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			
			request.setAttribute("errorMessage", e.getMessage());
			return ERROR_VIEW;
		}
    	return SINGLE_TASK_LIST_VIEW;
    }
	
	@RequestMapping(TASK_DETAIL)
    public void detail(HttpServletRequest request,ModelMap model) throws Exception {
		try{
			
			
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
	    	
	    	model.put("list", "");
		} catch (Exception e) {
			log.error("func[detail] parameterMap[{}] exception[{} - {}] desc[fail]",
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
	}
	
	/**
	 * 暂时不用实现
	 * @param request
	 * @param model
	 * @throws Exception
	 */
	@RequestMapping(TASK_CLAIME)
    public void claime(HttpServletRequest request,ModelMap model) throws Exception {
		try{
			
			
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
	    	
	    	model.put("list", "");
		} catch (Exception e) {
			log.error("func[claime] parameterMap[{}] exception[{} - {}] desc[fail]",
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
	}
	
	@RequestMapping(TASK_FINISH)
    public JSONObject finish(HttpServletRequest request,ModelMap model) throws Exception {
		try{
			
			HttpRequestInfo reqInfo = new HttpRequestInfo(request);
			Integer taskInfoId = reqInfo.getIntParameter("taskInfoId", 1);
			Integer taskCompleteInfoId = reqInfo.getIntParameter("taskCompleteInfoId", 1);
			Integer taskType = reqInfo.getIntParameter("taskType", 1);
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			JSONObject result = taskService.completeTask(user.getId(), taskCompleteInfoId, taskType, taskInfoId);
	    	return result;
		} catch (Exception e) {
			log.error("func[finish] parameterMap[{}] exception[{} - {}] desc[fail]",
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
	
	@RequestMapping(SINGLE_TASK_PROGRESS)
	public void getPointTaskInfoList(HttpServletRequest request,ModelMap model) throws Exception {
		try{
			setRequestModelMap(request, model);
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			int taskId = requestInfo.getIntParameter("taskId", 0);
			
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			List<Map<String, Object>> list = taskService.getPointTaskInfoProgressList(taskId, user.getId());
			model.put("list", list);
		} catch (Exception e) {
			log.error("func[finish] parameterMap[{}] exception[{} - {}] desc[fail]",
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
	}
	@RequestMapping(TASK_COMPLETE_RATE)
    public JSONObject taskCompleteRate(HttpServletRequest request,ModelMap model) throws Exception {
		try{
			
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			JSONObject result = taskService.taskCompleteRate(user.getId(), user.getUserType());
	    	return result;
		} catch (Exception e) {
			log.error("func[taskCompleteRate] parameterMap[{}] exception[{} - {}] desc[fail]",
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
	@RequestMapping(GET_TASK)
	public JSONObject getTask(HttpServletRequest request,ModelMap model) throws Exception {
		try{
			setRequestModelMap(request, model);
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			int taskId = requestInfo.getIntParameter("taskId", 0);
			
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			JSONObject result = taskService.getTask(user.getId(), taskId);
			return result;
		} catch (Exception e) {
			log.error("func[getTask] parameterMap[{}] exception[{} - {}] desc[fail]",
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
}
